From f1759e979781c37f3e3f8c5648cd22ffbf5f168b Mon Sep 17 00:00:00 2001 From: "Panashe M. Fundira" Date: Sun, 10 Jul 2016 13:29:31 -0400 Subject: [PATCH] Warn about duplicated build targets --- src/cargo/util/toml.rs | 16 ++++++++++++++++ tests/build.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 158fbdc22..5f35e3dd6 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -535,6 +535,10 @@ impl TomlManifest { debug!("manifest has no build targets"); } + if let Err(e) = unique_build_targets(&targets, layout) { + bail!("duplicate build target found: `{}`", e); + } + let mut deps = Vec::new(); let replace; @@ -752,6 +756,18 @@ fn unique_names_in_targets(targets: &[TomlTarget]) -> Result<(), String> { Ok(()) } +/// Will check a list of build targets, and make sure the target names are unique within a vector. +/// If not, the name of the offending build target is returned. +fn unique_build_targets(targets: &[Target], layout: &Layout) -> Result<(), String> { + let mut seen = HashSet::new(); + for v in targets.iter().map(|e| layout.root.join(e.src_path())) { + if !seen.insert(v.clone()) { + return Err(v.display().to_string()); + } + } + Ok(()) +} + impl TomlDependency { fn to_dependency(&self, name: &str, diff --git a/tests/build.rs b/tests/build.rs index c45d50660..5f2f67913 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -101,6 +101,39 @@ Caused by: src[..]Cargo.toml:1:5-1:6 expected a value\n\n")) } +#[test] +fn cargo_compile_duplicate_build_targets() { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + + [lib] + name = "main" + crate-type = ["dylib"] + + [dependencies] + "#) + .file("src/main.rs", r#" + fn main() {} + "#); + + let error_message = format!("\ +[ERROR] failed to parse manifest at `[..]` + +Caused by: + duplicate build target found: `{}`", + p.root().join("src[..]main.rs").display()); + + assert_that(p.cargo_process("build"), + execs() + .with_status(101) + .with_stderr(error_message) + ) +} + #[test] fn cargo_compile_with_invalid_version() { let p = project("foo") -- 2.30.2